<?php
namespace app\commands;

use app\models\Manager;
use Yii;
use yii\console\Controller;
use yii\helpers\Console;

/**
 * 权限初始化
 *
 * Class RbacController
 * @package app\commands
 */
class RbacController extends Controller
{
    /**
     * 权限初始化
     */
    public function actionInit()
    {
        $this->stdout("此脚本将重新初始化RBAC权限系统，\n", Console::FG_RED);
        $this->stdout("并按照角色重新分配原始权限，\n", Console::FG_RED);
        $this->stdout("初始化后需要系统管理员到后台重新给角色分配权限，\n", Console::FG_RED);
        if (!Console::confirm("请确认：")) {
            return;
        }

        $auth = Yii::$app->authManager;

        $this->stdout("删除全部权限分配。\n", Console::FG_RED);
        $auth->removeAllAssignments();
        $this->stdout("删除全部权限项。\n", Console::FG_RED);
        $auth->removeAll();

        $this->stdout("初始化RBAC权限数据。\n", Console::FG_YELLOW);
        $rbac       = $this->_initRbac();
        $db         = $this->_initDb();
        $user       = $this->_initUser();
        $ad         = $this->_initAd();
        $article    = $this->_initArticle();
        $statistics = $this->_initStatistics();
        $system     = $this->_initSystem();

        $this->stdout("创建系统管理员角色：", Console::FG_YELLOW);
        $user_admin = $auth->createRole('role_1');
        $user_admin->description = '系统管理员';
        $auth->add($user_admin);
        $auth->addChild($user_admin, $rbac);
        $auth->addChild($user_admin, $db);
        $auth->addChild($user_admin, $user);
        $auth->addChild($user_admin, $ad);
        $auth->addChild($user_admin, $article);
        $auth->addChild($user_admin, $statistics);
        $auth->addChild($user_admin, $system);
        $this->stdout("完成。\n", Console::FG_GREEN);

        foreach (Manager::find()
                     ->andWhere(['status'=>Manager::STATUS_ACTIVE, 'rid'=>1])
                     ->each() as $admin) {
            $this->stdout("给管理员[{$admin->id}]分配系统管理员角色权限：", Console::FG_GREEN);
            $role = $auth->getRole('role_1');
            $auth->assign($role, $admin->id);
            $this->stdout("完成。\n", Console::FG_GREEN);
        }
        $this->stdout("权限初始化完成。\n", Console::FG_GREEN);
    }

    /**
     * 权限相关
     */
    private function _initRbac()
    {
        $auth = Yii::$app->authManager;

        $this->stdout("创建权限 rbac/menu:", Console::FG_YELLOW);
        $rbac_menu = $auth->createPermission('rbac/menu');
        $rbac_menu->description = '权限管理菜单';
        $auth->add($rbac_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 rbac/manager:", Console::FG_YELLOW);
        $rbac_manager = $auth->createPermission('rbac/manager');
        $rbac_manager->description = '管理员管理';
        $auth->add($rbac_manager);
        $auth->addChild($rbac_manager, $rbac_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 rbac/role:", Console::FG_YELLOW);
        $rbac_role = $auth->createPermission('rbac/role');
        $rbac_role->description = '角色管理';
        $auth->add($rbac_role);
        $auth->addChild($rbac_role, $rbac_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 rbac/item:", Console::FG_YELLOW);
        $rbac_item = $auth->createPermission('rbac/item');
        $rbac_item->description = '权限管理';
        $auth->add($rbac_item);
        $auth->addChild($rbac_item, $rbac_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 rbac/log:", Console::FG_YELLOW);
        $rbac_log = $auth->createPermission('rbac/log');
        $rbac_log->description = '管理日志';
        $auth->add($rbac_log);
        $auth->addChild($rbac_log, $rbac_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 rbac:", Console::FG_YELLOW);
        $rbac = $auth->createPermission('rbac');
        $rbac->description = '权限管理';
        $auth->add($rbac);
        $auth->addChild($rbac, $rbac_manager);
        $auth->addChild($rbac, $rbac_role);
        $auth->addChild($rbac, $rbac_item);
        $auth->addChild($rbac, $rbac_log);
        $this->stdout("完成。\n", Console::FG_GREEN);

        return $rbac;
    }

    /**
     * 数据库相关
     */
    private function _initDb()
    {
        $auth = Yii::$app->authManager;

        $this->stdout("创建权限 db/menu:", Console::FG_YELLOW);
        $db_menu = $auth->createPermission('db/menu');
        $db_menu->description = '数据管理菜单';
        $auth->add($db_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 db/sql:", Console::FG_YELLOW);
        $db_sql = $auth->createPermission('db/sql');
        $db_sql->description = '执行SQL语句';
        $auth->add($db_sql);
        $auth->addChild($db_sql, $db_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 db/backup:", Console::FG_YELLOW);
        $db_backup = $auth->createPermission('db/backup');
        $db_backup->description = '数据库备份';
        $auth->add($db_backup);
        $auth->addChild($db_backup, $db_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 db:", Console::FG_YELLOW);
        $db = $auth->createPermission('db');
        $db->description = '数据库管理';
        $auth->add($db);
        $auth->addChild($db, $db_sql);
        $auth->addChild($db, $db_backup);
        $this->stdout("完成。\n", Console::FG_GREEN);

        return $db;
    }

    /**
     * 用户相关
     */
    private function _initUser()
    {
        $auth = Yii::$app->authManager;

        $this->stdout("创建权限 user/menu:", Console::FG_YELLOW);
        $user_menu = $auth->createPermission('user/menu');
        $user_menu->description = '用户管理菜单';
        $auth->add($user_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 user/list:", Console::FG_YELLOW);
        $user_list = $auth->createPermission('user/list');
        $user_list->description = '用户列表';
        $auth->add($user_list);
        $auth->addChild($user_list, $user_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 user/edit:", Console::FG_YELLOW);
        $user_edit = $auth->createPermission('user/edit');
        $user_edit->description = '用户修改';
        $auth->add($user_edit);
        $auth->addChild($user_edit, $user_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 user/status:", Console::FG_YELLOW);
        $user_status = $auth->createPermission('user/status');
        $user_status->description = '设置用户状态';
        $auth->add($user_status);
        $auth->addChild($user_status, $user_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 user:", Console::FG_YELLOW);
        $user = $auth->createPermission('user');
        $user->description = '用户管理';
        $auth->add($user);
        $auth->addChild($user, $user_list);
        $auth->addChild($user, $user_edit);
        $auth->addChild($user, $user_status);
        $this->stdout("完成。\n", Console::FG_GREEN);

        return $user;
    }

    /**
     * 广告相关
     */
    private function _initAd()
    {
        $auth = Yii::$app->authManager;

        $this->stdout("创建权限 ad/menu:", Console::FG_YELLOW);
        $ad_menu = $auth->createPermission('ad/menu');
        $ad_menu->description = '广告管理菜单';
        $auth->add($ad_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 ad/list:", Console::FG_YELLOW);
        $ad_list = $auth->createPermission('ad/list');
        $ad_list->description = '广告列表';
        $auth->add($ad_list);
        $auth->addChild($ad_list, $ad_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 ad/edit:", Console::FG_YELLOW);
        $ad_edit = $auth->createPermission('ad/edit');
        $ad_edit->description = '广告修改';
        $auth->add($ad_edit);
        $auth->addChild($ad_edit, $ad_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 ad/location:", Console::FG_YELLOW);
        $ad_location = $auth->createPermission('ad/location');
        $ad_location->description = '广告位置';
        $auth->add($ad_location);
        $auth->addChild($ad_location, $ad_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 ad:", Console::FG_YELLOW);
        $ad = $auth->createPermission('ad');
        $ad->description = '广告管理';
        $auth->add($ad);
        $auth->addChild($ad, $ad_list);
        $auth->addChild($ad, $ad_edit);
        $auth->addChild($ad, $ad_location);
        $this->stdout("完成。\n", Console::FG_GREEN);

        return $ad;
    }

    /**
     * 资讯相关
     */
    private function _initArticle()
    {
        $auth = Yii::$app->authManager;

        $this->stdout("创建权限 article/menu:", Console::FG_YELLOW);
        $article_menu = $auth->createPermission('article/menu');
        $article_menu->description = '资讯管理菜单';
        $auth->add($article_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 article/list:", Console::FG_YELLOW);
        $article_list = $auth->createPermission('article/list');
        $article_list->description = '资讯列表';
        $auth->add($article_list);
        $auth->addChild($article_list, $article_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 article/edit:", Console::FG_YELLOW);
        $article_edit = $auth->createPermission('article/edit');
        $article_edit->description = '资讯编辑';
        $auth->add($article_edit);
        $auth->addChild($article_edit, $article_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 article/category:", Console::FG_YELLOW);
        $article_category = $auth->createPermission('article/category');
        $article_category->description = '资讯分类';
        $auth->add($article_category);
        $auth->addChild($article_category, $article_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 article:", Console::FG_YELLOW);
        $article = $auth->createPermission('article');
        $article->description = '资讯管理';
        $auth->add($article);
        $auth->addChild($article, $article_edit);
        $auth->addChild($article, $article_list);
        $auth->addChild($article, $article_category);
        $this->stdout("完成。\n", Console::FG_GREEN);

        return $article;
    }

    /**
     * 统计分析相关
     */
    private function _initStatistics()
    {
        $auth = Yii::$app->authManager;

        $this->stdout("创建权限 statistics/menu:", Console::FG_YELLOW);
        $statistics_menu = $auth->createPermission('statistics/menu');
        $statistics_menu->description = '统计分析菜单';
        $auth->add($statistics_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 statistics/user:", Console::FG_YELLOW);
        $statistics_user = $auth->createPermission('statistics/user');
        $statistics_user->description = '用户统计';
        $auth->add($statistics_user);
        $auth->addChild($statistics_user, $statistics_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 statistics/article:", Console::FG_YELLOW);
        $statistics_article = $auth->createPermission('statistics/article');
        $statistics_article->description = '资讯访问统计';
        $auth->add($statistics_article);
        $auth->addChild($statistics_article, $statistics_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 statistics:", Console::FG_YELLOW);
        $statistics = $auth->createPermission('statistics');
        $statistics->description = '统计分析';
        $auth->add($statistics);
        $auth->addChild($statistics, $statistics_user);
        $auth->addChild($statistics, $statistics_article);
        $this->stdout("完成。\n", Console::FG_GREEN);

        return $statistics;
    }

    /**
     * 系统相关
     */
    private function _initSystem()
    {
        $auth = Yii::$app->authManager;

        $this->stdout("创建权限 system/menu:", Console::FG_YELLOW);
        $system_menu = $auth->createPermission('system/menu');
        $system_menu->description = '系统管理菜单';
        $auth->add($system_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 system/config:", Console::FG_YELLOW);
        $system_config = $auth->createPermission('system/config');
        $system_config->description = '系统设置';
        $auth->add($system_config);
        $auth->addChild($system_config, $system_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 system/message:", Console::FG_YELLOW);
        $system_message = $auth->createPermission('system/message');
        $system_message->description = '群发消息';
        $auth->add($system_message);
        $auth->addChild($system_message, $system_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 system/task:", Console::FG_YELLOW);
        $system_task = $auth->createPermission('system/task');
        $system_task->description = '定时任务';
        $auth->add($system_task);
        $auth->addChild($system_task, $system_menu);
        $this->stdout("完成。\n", Console::FG_GREEN);

        $this->stdout("创建权限 system:", Console::FG_YELLOW);
        $system = $auth->createPermission('system');
        $system->description = '系统管理';
        $auth->add($system);
        $auth->addChild($system, $system_config);
        $auth->addChild($system, $system_message);
        $auth->addChild($system, $system_task);
        $this->stdout("完成。\n", Console::FG_GREEN);

        return $system;
    }
}
